package com.gisuni.sddc.platformManager.service;

import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import com.gisuni.sddc.platformManager.dao.RoleDao;
import com.gisuni.sddc.platformManager.dao.ServerInfoDao;
import com.gisuni.sddc.platformManager.model.PageInfo;
import com.gisuni.sddc.platformManager.model.Response;
import com.gisuni.sddc.platformManager.model.ServerInfo;
import com.gisuni.sddc.platformManager.model.Tree;
import com.gisuni.sddc.util.StringUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

@IocBean
public class ServerInfoService   {

	private Log log = Logs.getLog(ServerInfoService.class);    
    @Inject
    private ServerInfoDao serverInfoDao;
  
 
	public Response queryBaseServerInfoAll(int from, int to, String name) {
		Response response = new Response();
		response.setSuccess(false);
		String Result=null;
 
		try {
			String result = serverInfoDao.queryBaseServerInfoAll(  from,  to,  name);
			int total = serverInfoDao.queryBaseServerInfoCount( name);
			response.setSuccess(true);
			response.setResult(result);
			response.setTotal(total);
			return response;
		}catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			response.setMsg("查询失败");
			return response;
		}
		
	}

  
    public ServerInfo findRoleById(String id) {
      return serverInfoDao.findServerInfoById(id);
    }

    

	public Response addServerInfo(HttpServletRequest request) {
   	 Response res = new Response();
   	 res.setSuccess(false);
		 String serverName = request.getParameter("name");
		 String type = request.getParameter("type");
		 String dynamicsUrl = request.getParameter("dynamicsUrl");
		 String featureUrl = request.getParameter("featureUrl");
		 String  status = request.getParameter("status");
		 String  description = request.getParameter("description");
		 //TODO 判断
		 String id = UUID.randomUUID().toString().replaceAll("-", "");
		 ServerInfo serverInfo = new ServerInfo();
		 serverInfo.setServerName(serverName);
		 serverInfo.setType(type);
		 serverInfo.setStatus(status);
		 serverInfo.setDynamicsUrl(dynamicsUrl);
		 serverInfo.setFeatureUrl(featureUrl); 
		 serverInfo.setDescription(description);
		 serverInfo.setCreateTime(new Date());
		 serverInfo.setId(id);
 
		 boolean flag  = serverInfoDao.addServerInfo(serverInfo);
		 if(flag){
			 res.setSuccess(true);
			 res.setMsg(id);
		 } 
		 
		return res;
	}
	
  
    public Response updateServerInfo(HttpServletRequest request) {
      	 Response res = new Response();
       	 res.setSuccess(false);
   	     String serverName = request.getParameter("name");
		 String id = request.getParameter("id");
		 String status = request.getParameter("status");
		 String description = request.getParameter("description");
		 
		 ServerInfo serverInfo = new ServerInfo();
		 serverInfo.setServerName(serverName);
		 serverInfo.setStatus(status);
		 serverInfo.setId(id);
		 serverInfo.setUpdateTime(new Date());
		 serverInfo.setDescription(description);
		 
		 
		 boolean flag  = serverInfoDao.updateServerInfo(serverInfo);
		 if(flag){
			 res.setSuccess(true);
		 } 
		 
		return res;
    }

    public Response deleteServerInfoById(String id) {
   	 Response res = new Response();
   	 res.setSuccess(false);
   	 try{
   		 //删除对应的图层
   		boolean flag = serverInfoDao.execuleSQL("delete from SDDCGISPLATFORM.LAYER_INFO where SERVER_ID='"+id+"'");
   		 //删除服务
   		if(flag){
   	     	ServerInfo serverInfo = new ServerInfo();
   	     	serverInfo.setId(id);
   	    	 if(serverInfoDao.delServerInfo(serverInfo)){
   	    		 res.setSuccess(true);
   	    	 }
   		}
    	 return res;
   	 }catch(Exception e){
   		 e.printStackTrace();
   		 log.error(e);
   		 res.setMsg("操作失败");
   		 return res;
   	 }
    }

}
